<?php
class AppController extends Controller {
	var $components = array ('Session','email'); //,'Auth'
	var $uses = array('User','SiteSetting');
	var $settings;
    var $anonymousAccess = array();
	function beforeFilter()
	{
	   if(!in_array($this->params['action'],$this->anonymousAccess))
	   {
			if(isset($this->params['admin']) && $this->params['admin']=='1')
			{
				$this->layout = 'admin';
				if($this->Session->read('User.id')!='1')
				{
					$this->redirect('/users/login');
				}
			}

	   		if($this->Session->read('User.id')=='')
			{
				$this->redirect('/users/signup');
			}
	   }
	   //$this->Auth->checkSession();
	   $this->settings = $this->SiteSetting->getAll();
       $this->set('site_settings',$this->settings);	
       //debug($this->settings);
	}   

	protected function calculatMarks($user_id)
	{
        //$user_id = $this->Session->read('User.id');
		$batch = $this->UsersBatch->getBatch($user_id);
		
		$parts = $this->Part->getAllParts($batch['UsersBatch']['batch_id']);
		$questionIds = array();
		$failed=false;
		foreach($parts as $part)
		{
			$fulltotal[$part['Part']['id']] = 0;
			foreach($part['Question'] as $question)
			{
				$questionIds[$part['Part']['id']][] = $question['id'];
				$fulltotal[$part['Part']['id']] = $fulltotal[$part['Part']['id']] + $question['mark'];
			}		
			
			$totals[$part['Part']['id']] =  $this->Answer->sumOfQuestions($questionIds[$part['Part']['id']],$user_id);
			//if($totals[$part['Part']['id']]<$part['Part']['min_mark']) $failed = true;
		}	
		$total_mark_scored = array_sum($totals);
		if($batch['Batch']['min_mark']>$total_mark_scored)
		{
			$failed = true;
		}
		return array($totals,$parts,$failed,$fulltotal,$batch);	
	}    
}
?>
